package pc

import (
	. "chess_platform/internal/common"
	"chess_platform/internal/login/fetch"
	"chess_platform/models/pc"
	"fmt"
)

//[pc]配置中心
type ConfController struct {
	BaseController
}

func (b *ConfController) URLMapping() {
	b.Mapping("ReloadConf", b.ReloadConf)
}

// @Title 重新加载配置
// @Description 重新加载配置
// @Success 0 {string} token
// @Failure 400
// @Failure 404
// @router /reload-conf [get]
func (b *ConfController) ReloadConf() {
	LogPc.Debug("-----------pc-------reload-----------conf---")
	//加载系统配置
	if !ReloadConfigFromMysql() {
		b.ResponseFail("从mysql加载配置失败,请检查")
	}
	//InitAlgorithm()
	b.ResponseSuccess()
}

//发送消息给部署各地的客户端,H5端,PC端(包括自己)重新加载配置
func SendMsgToReloadConf() []string {
	var Errors []string
	ap, err := pc.ListAllPlatform()
	if err != nil {
		Errors = append(Errors, err.Error())
		return Errors
	}
	pcInterface := "/v1/pc/conf/reload-conf"
	h5Interface := "/v1/h5/conf/reload-conf"
	clientInterface := "/v1/client/conf/reload-conf"
	for _, v := range ap {
		LogPc.Debug(fmt.Sprintf("IP:%v,Port:%v", v.IP, v.HttpPort))
		LogPc.Debug(fmt.Sprintf("type:%v", v.Type))
		switch v.Type {
		case PlatformPc:
			LogPc.Debug(fmt.Sprintf("PC端,status:%v,excuteTask:%v", v.Status, v.ExecuteTask))
			url := fmt.Sprintf("http://%v:%v%v", v.IP, v.HttpPort, pcInterface)
			_, err := fetch.Cmd(fetch.Request{
				URL:    url,
				Method: "GET",
			})
			if err != nil {
				msg := fmt.Errorf("访问%v失败:%v\n", url, err)
				Errors = append(Errors, msg.Error())
				LogPc.Error(msg.Error())
			}
		case PlatformH5:
			LogPc.Debug(fmt.Sprintf("H5端,status:%v,excuteTask:%v", v.Status, v.ExecuteTask))
			url := fmt.Sprintf("http://%v:%v%v", v.IP, v.HttpPort, h5Interface)
			_, err := fetch.Cmd(fetch.Request{
				URL:    url,
				Method: "GET",
			})
			if err != nil {
				msg := fmt.Errorf("访问%v失败:%v\n", url, err)
				Errors = append(Errors, msg.Error())
				LogPc.Error(msg.Error())
			}
		case PlatformClient:
			LogPc.Debug(fmt.Sprintf("client端,status:%v,excuteTask:%v", v.Status, v.ExecuteTask))
			url := fmt.Sprintf("http://%v:%v%v", v.IP, v.HttpPort, clientInterface)
			_, err := fetch.Cmd(fetch.Request{
				URL:    url,
				Method: "GET",
			})
			if err != nil {
				msg := fmt.Errorf("访问%v失败:%v\n", url, err)
				Errors = append(Errors, msg.Error())
				LogPc.Error(msg.Error())
			}
		}
	}
	return Errors
}

//发送消息给部署各地的客户端,H5端 重新初始化支付比例
func SendMsgToInitAlgorithm() []string {
	var Errors []string
	ap, err := pc.ListAllPlatform()
	if err != nil {
		Errors = append(Errors, err.Error())
		return Errors
	}
	h5Interface := "/v1/h5/conf/init-algorithm"
	clientInterface := "/v1/client/conf/init-algorithm"

	for _, v := range ap {
		LogPc.Debug(fmt.Sprintf("IP:%v,Port:%v", v.IP, v.HttpPort))
		LogPc.Debug(fmt.Sprintf("type:%v", v.Type))
		switch v.Type {
		case PlatformH5:
			LogPc.Debug(fmt.Sprintf("H5端,status:%v,excuteTask:%v", v.Status, v.ExecuteTask))
			url := fmt.Sprintf("http://%v:%v%v", v.IP, v.HttpPort, h5Interface)
			_, err := fetch.Cmd(fetch.Request{
				URL:    url,
				Method: "GET",
			})
			if err != nil {
				msg := fmt.Errorf("访问%v失败:%v\n", url, err)
				Errors = append(Errors, msg.Error())
				LogPc.Error(msg.Error())
			}
		case PlatformClient:
			LogPc.Debug(fmt.Sprintf("client端,status:%v,excuteTask:%v", v.Status, v.ExecuteTask))
			url := fmt.Sprintf("http://%v:%v%v", v.IP, v.HttpPort, clientInterface)
			_, err := fetch.Cmd(fetch.Request{
				URL:    url,
				Method: "GET",
			})
			if err != nil {
				msg := fmt.Errorf("访问%v失败:%v\n", url, err)
				Errors = append(Errors, msg.Error())
				LogPc.Error(msg.Error())
			}
		}
	}
	return nil
}

//发送消息给部署各地的客户端,H5端 重新初始化平台服务器状态
func SendMsgToInitPlatformConf() []string {
	var Errors []string
	ap, err := pc.ListAllPlatform()
	if err != nil {
		Errors = append(Errors, err.Error())
		return Errors
	}
	h5Interface := "/v1/h5/conf/init-platform-conf"
	clientInterface := "/v1/client/conf/init-platform-conf"
	pfInterface := "/v1/pf/conf/init-platform-conf"
	pbInterface := "/v1/pb/conf/init-platform-conf"

	for _, v := range ap {
		LogPc.Debug(fmt.Sprintf("IP:%v,Port:%v", v.IP, v.HttpPort))
		LogPc.Debug(fmt.Sprintf("type:%v", v.Type))
		switch v.Type {
		case PlatformH5:
			LogPc.Debug(fmt.Sprintf("H5端,status:%v,excuteTask:%v", v.Status, v.ExecuteTask))
			url := fmt.Sprintf("http://%v:%v%v", v.IP, v.HttpPort, h5Interface)
			_, err := fetch.Cmd(fetch.Request{
				URL:    url,
				Method: "GET",
			})
			if err != nil {
				msg := fmt.Errorf("访问%v失败:%v\n", url, err)
				Errors = append(Errors, msg.Error())
				LogPc.Error(msg.Error())
			}
		case PlatformClient:
			LogPc.Debug(fmt.Sprintf("client端,status:%v,excuteTask:%v", v.Status, v.ExecuteTask))
			url := fmt.Sprintf("http://%v:%v%v", v.IP, v.HttpPort, clientInterface)
			_, err := fetch.Cmd(fetch.Request{
				URL:    url,
				Method: "GET",
			})
			if err != nil {
				msg := fmt.Errorf("访问%v失败:%v\n", url, err)
				Errors = append(Errors, msg.Error())
				LogPc.Error(msg.Error())
			}
		case PlatformPf:
			LogPc.Debug(fmt.Sprintf("pf端,status:%v,excuteTask:%v", v.Status, v.ExecuteTask))
			url := fmt.Sprintf("http://%v:%v%v", v.IP, v.HttpPort, pfInterface)
			_, err := fetch.Cmd(fetch.Request{
				URL:    url,
				Method: "GET",
			})
			if err != nil {
				msg := fmt.Errorf("访问%v失败:%v\n", url, err)
				Errors = append(Errors, msg.Error())
				LogPc.Error(msg.Error())
			}
		case PlatformPb:
			LogPc.Debug(fmt.Sprintf("pb端,status:%v,excuteTask:%v", v.Status, v.ExecuteTask))
			url := fmt.Sprintf("http://%v:%v%v", v.IP, v.HttpPort, pbInterface)
			_, err := fetch.Cmd(fetch.Request{
				URL:    url,
				Method: "GET",
			})
			if err != nil {
				msg := fmt.Errorf("访问%v失败:%v\n", url, err)
				Errors = append(Errors, msg.Error())
				LogPc.Error(msg.Error())
			}
		}
	}
	return nil
}
